package com.itic.appbase.applications.sys.common.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.applications.sys.code.persistence.model.CodeDetail;
import com.itic.appbase.applications.sys.code.vo.CodeDetailVo;
import com.itic.appbase.applications.sys.common.service.OutCommonService;
import com.itic.appbase.applications.sys.org.persistence.model.Org;
import com.itic.appbase.applications.sys.region.persistence.model.Region;
import com.itic.appbase.applications.sys.region.service.RegionService;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.constants.InitConst;
import com.itic.appbase.framework.common.constants.IticConst;
import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.common.controller.BaseController;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.appbase.framework.utils.tree.TreeHelper;
import com.itic.appbase.framework.utils.tree.TreeNode;

/**
 * 
 * <p>Company: itic</p>
 * 
 * @author: shijun
 * @date: 2015年6月13日 上午9:34:47
 * @version: V1.0
 */
@Controller
@RequestMapping(value = "/sys/out")
public class OutCommonController
        extends BaseController {

    @Autowired
    private OutCommonService outCommonService;
    
    @Autowired
    private RegionService regionService;

    /**
     * 跳转到行政区划公共页面
     * 
     * @return
     */
    @RequestMapping("/initRegion")
    public String initRegion() {
        return "sys/common/region";
    }

    /**
     * 跳转到组织机构公共页面
     * 
     * @return
     */
    @RequestMapping("/initOrg")
    public String initOrg() {
        return "sys/common/org";
    }

    /**
     * 获取代码表信息
     * 
     * @param codeDetailVo
     *            </br>type :代码表类型，如T_DM_SEX filterCode :需要过滤的代码
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getCodeDetail")
    @ResponseBody
    public List<CodeDetailVo> getCodeDetail(CodeDetailVo codeDetailVo) {
        // 声明返回对象
        List<CodeDetailVo> codeDetailVoList = new ArrayList<CodeDetailVo>();
        // 获取数据
        List<CodeDetail> codeDetailList = outCommonService.getCodeDetail(codeDetailVo);
        // 封装数据
        if (null != codeDetailList && !codeDetailList.isEmpty()) {
            // 封装数据
            for (CodeDetail codeDetail : codeDetailList) {
                CodeDetailVo vo = new CodeDetailVo(codeDetail.getId(), codeDetail.getParentId(), codeDetail.getCode(),
                        codeDetail.getName());
                codeDetailVoList.add(vo);
            }
        }
        return codeDetailVoList;
    }

    /**
     * 获取整棵组织机构树
     * 
     * @return
     */
    @RequestMapping(value = "/getOrgTree/all")
    @ResponseBody
    public Map<String, Object> getAllOrgTree() {
        // 声明返回对象
        Map<String, Object> resultMap = new HashMap<String, Object>();
        // 查询条件
        Org queryOrg = new Org();
        queryOrg.setIsEffective(DBConst.TRUE);
        queryOrg.setpCode(InitConst.P_ID);// 获取组织机构树的根节点-1
        // 获取数据
        List<Org> list = outCommonService.getOrgTree(queryOrg);
        // 封装数据
        resultMap.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
        resultMap.put(IticConst.DATA, list);
        return resultMap;
    }

    /**
     * 获取本下级组织机构树
     * <p>
     * 根据code获取组织机构树
     * </p>
     * 
     * @param org
     *            </br>code：组织机构代码；clazz:组织机构类别
     * @return
     */
    @RequestMapping(value = "/getOrgTree/bxj")
    @ResponseBody
    public Map<String, Object> getBxjOrgTree(Org org) {
        // 返回声明对象
        Map<String, Object> resultMap = new HashMap<String, Object>();
        // 封装查询组织机构code
        Org queryOrg = new Org();
        queryOrg.setIsEffective(DBConst.TRUE);
        queryOrg.setCode(org.getCode());

        if (StringHelper.isNotBlank(org.getClazz())) {
            queryOrg.setClazz(org.getClazz());
        }

        // 查询数据
        List<Org> list = outCommonService.getOrgTree(queryOrg);
        // 封装数据
        resultMap.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
        resultMap.put(IticConst.DATA, list);
        return resultMap;
    }

    /**
     * 获取下级组织机构树
     * <p>
     * 根据pCode获取组织机构树
     * </p>
     * 
     * @param org
     *            </br>pCode：上级组织机构代码；clazz:组织机构类别
     * @return
     */
    @RequestMapping(value = "/getOrgTree/xj")
    @ResponseBody
    public Map<String, Object> getXjOrgTree(Org org) {
        // 返回声明对象
        Map<String, Object> resultMap = new HashMap<String, Object>();
        // 封装查询组织机构code
        Org queryOrg = new Org();
        queryOrg.setIsEffective(DBConst.TRUE);
        queryOrg.setpCode(org.getpCode());

        if (StringHelper.isNotBlank(org.getClazz())) {
            queryOrg.setClazz(org.getClazz());
        }

        // 查询数据
        List<Org> list = outCommonService.getOrgTree(queryOrg);
        // 封装数据
        resultMap.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
        resultMap.put(IticConst.DATA, list);
        return resultMap;
    }

    /**
     * 获取当前登录者的本下级组织机构树
     * 
     * @param request
     * @return
     */
    @RequestMapping(value = "/getOrgTree")
    @ResponseBody
    public Map<String, Object> getOrgTree(HttpServletRequest request) {
        // 返回声明对象
        Map<String, Object> resultMap = new HashMap<String, Object>();
        // 封装查询组织机构code
        Org queryOrg = new Org();
        queryOrg.setIsEffective(DBConst.TRUE);
        queryOrg.setCode(this.findUserSession(request).getOrganizationCode());// 查询本下级的组织机构信息

        // 查询数据
        List<Org> list = outCommonService.getOrgTree(queryOrg);
        // 封装数据
        resultMap.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
        resultMap.put(IticConst.DATA, list);
        return resultMap;
    }

    /**
     * 获取所有行政区划树
     * 
     * @return
     */
    @RequestMapping(value = "/getRegionTree/all")
    @ResponseBody
    public Map<String, Object> getAllRegionTree() {
        // 返回声明对象
        Map<String, Object> resultMap = new HashMap<String, Object>();
        // 封装查询条件
        Region region = new Region();
        region.setIsEffective(DBConst.TRUE);
        region.setpId(InitConst.P_ID);// 获取行政区划树的根节点-1
        // 查询数据
        List<Region> data = outCommonService.getRegionTree(region);
        resultMap.put(IticConst.DATA, data);
        resultMap.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
        return resultMap;
    }

    /**
     * 获取本下级行政区划树
     * <p>
     * 根据查询条件id获取本下级树
     * </p>
     * 
     * @param id
     *            本级节点id
     * @return
     */
    @RequestMapping(value = "/getRegionTree/bxj")
    @ResponseBody
    public Map<String, Object> getBxjRegionTree(Region r) {
        // 返回声明对象
        Map<String, Object> resultMap = new HashMap<String, Object>();
        // 封装查询条件
        Region region = new Region();
        region.setIsEffective(DBConst.TRUE);
        region.setId(r.getId());
        if (null != r && StringHelper.isNotBlank(r.getType())){
           region.setType(r.getType()); 
        }
        // 查询数据
        List<Region> data = outCommonService.getRegionTree(region);

        resultMap.put(IticConst.DATA, data);
        resultMap.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
        return resultMap;
    }

    /**
     * 获取下级行政区划树
     * <p>
     * 根据pId获取下级行政区划树
     * </p>
     * 
     * @param pId
     *            上级节点id
     * @param request
     * @return
     */
    @RequestMapping(value = "/getRegionTree/xj")
    @ResponseBody
    public Map<String, Object> getXjRegionTree(Region r) {
        // 返回声明对象
        Map<String, Object> resultMap = new HashMap<String, Object>();
        // 封装查询条件
        Region region = new Region();
        region.setIsEffective(DBConst.TRUE);
        region.setpId(r.getId());
        if (null != r && StringHelper.isNotBlank(r.getType())){
            region.setType(r.getType()); 
         }
        // 查询数据
        List<Region> data = outCommonService.getRegionTree(region);

        resultMap.put(IticConst.DATA, data);
        resultMap.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
        return resultMap;
    }

    /**
     * 获取当前登录人的本下级行政区划树
     * 
     * @param request
     * @return
     */
    @RequestMapping(value = "/getRegionTree")
    @ResponseBody
    public Map<String, Object> getRegionTree(HttpServletRequest request, Region r) {
        // 返回声明对象
        Map<String, Object> resultMap = new HashMap<String, Object>();
        // 封装查询条件
        Region region = new Region();
        region.setIsEffective(DBConst.TRUE);
        Org org = outCommonService.getOrgByCode(this.findUserSession(request).getOrganizationCode());
        region.setId(org.getRegionId());
        if (null != r && StringHelper.isNotBlank(r.getType())){
            region.setType(r.getType()); 
         }
        // 查询数据
        List<Region> data = outCommonService.getRegionTree(region);

        resultMap.put(IticConst.DATA, data);
        resultMap.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
        return resultMap;
    }

    /**
     * 带拼音查询本下级行政区划数据{"name": "北京", children: []}
     * <p>行政区划按拼音查询数据</p>
     * 
     * @param rootId 根节点
     */
    @RequestMapping(value = "/getStdRegionData")
    @ResponseBody
    public Map<String, Object> getStandardRegionData(String id) {
        // 返回声明对象
        Map<String, Object> resultMap = new HashMap<String, Object>();

        // 查询数据
        Region rTemp = new Region();
        rTemp.setId(id);
        rTemp.setIsEffective(DBConst.TRUE);
        List<Region> regionList = outCommonService.getRegionTree(rTemp);

        List<TreeNode> treeList = new ArrayList<TreeNode>();
        // 代码转名称
        Map<String, String> codeMap = new HashMap<String, String>();
        codeMap.put("type", "T_DM_REGION_TYPE");

        for (Region region : regionList) {
            TreeNode node = new TreeNode(region.getId(), region.getId(), region.getpId(), region.getName());
            Map<String, Object> attributes = new HashMap<String, Object>();
            attributes.put("level", region.getType());
            region = (Region) outCommonService.getNameByCode(region, codeMap);
            attributes.put("type", region.getType());
            attributes.put("code", region.getCode());
            node.setAttributes(attributes);
            treeList.add(node);
        }
        List<TreeNode> dataList = TreeHelper.getTree(treeList);

        resultMap.put(IticConst.DATA, dataList);
        resultMap.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
        return resultMap;
    }
    
    
}
